package com.liuhaozzu.zookeeper.zookeeper_app.constructor;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class ZooKeeper_Demo1 implements Watcher{

	private static CountDownLatch connectedSemaphore=new CountDownLatch(1);
	
	public static void main(String[] args) throws IOException {
		ZooKeeper zooKeeper=new ZooKeeper("hadoop1:2181", 500, new ZooKeeper_Demo1());
		
		System.out.println(zooKeeper.getState());
		try {
			connectedSemaphore.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ZooKeeper session established.");
	
	}

	@Override
	public void process(WatchedEvent event) {
		System.out.println("Receive watched event: " +event);
		if (KeeperState.SyncConnected == event.getState()) {
			connectedSemaphore.countDown();
		}
	}

}
